package com.bugstack.domain.strategy.service.rule.chain.impl;

import com.bugstack.domain.strategy.repository.IStrategyRepository;
import com.bugstack.domain.strategy.service.rule.chain.AbstractLogicChain;
import com.bugstack.domain.strategy.service.rule.chain.factory.DefaultChainFactory;
import com.bugstack.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

/**
 * @Author: czb
 * @Description: 黑名单责任链
 * @DateTime: 2025/3/9 21:37
 **/
@Slf4j
@Component("rule_blacklist")
public class BlackListLogicChain extends AbstractLogicChain {

    @Resource
    private IStrategyRepository repository;

    @Override
    public DefaultChainFactory.StrategyAwardVO logic(String userId, Long strategyId) throws ExecutionException, InterruptedException {
        String ruleModel = DefaultChainFactory.LogicModel.RULE_BLACKLIST.getCode();
        log.info("抽奖责任链-黑名单开始 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel);

        String ruleValue = repository.queryStrategyRuleValue(strategyId, ruleModel);
        String[] splitRuleValue = ruleValue.split(Constants.COLON);
        Integer awardId = Integer.parseInt(splitRuleValue[0]);
        String[] blackUerList = splitRuleValue[1].split(Constants.SPLIT);
        for (String s : blackUerList) {
            if (userId.equals(s)) {
                log.info("抽奖责任链-黑名单接管 userId: {} strategyId: {} ruleModel: {} awardId: {}", userId, strategyId, ruleModel, awardId);
                return DefaultChainFactory.StrategyAwardVO.builder().awardId(awardId).logicModel(ruleModel).awardRuleValue("0.01,1").build();
            }
        }
        log.info("抽奖责任链-黑名单放行 userId: {} strategyId: {} ruleModel: {}", userId, strategyId, ruleModel);
        return next().logic(userId, strategyId);
    }
    
}
